<template>
	<view class="detail-wrap">
		<view class="card-item">
			<view v-if="detail.ImgList.length > 0" class="swiper-wrap">
				<swiper class="swiper-box" @change="swiperChange">
					<swiper-item v-for="(item, index) in detail.ImgList" :key="index" class="swipers flex-center">
						<image :src="item" mode="aspectFill" @click="ViewImage(index, detail.ImgList)" class="img">
						</image>
					</swiper-item>
				</swiper>
				<div class="custom-indicator flex-center">{{ current+1 }} / {{ detail.ImgList.length }}</div>
			</view>
			<view class="content">
				<view class="item-head">
					<view class="left-info">
						<view class="img-wrap container">
							<image :src="detail.AvatarUrl"
								mode="widthFix" class="avatar"></image>
						</view>
						<view class="info">
							<view class="head-name">{{ detail.PublishName }}</view>
							<view class="head-create">{{ detail.CreateDateUtc }}</view>
						</view>
					</view>
				</view>
				<view class="text-content">{{detail.Content}}</view>

				<view class="row bottom-btn">
					<view class="btn-item row_align_center" @tap.stop="handleLike(detail)">
						<li :class="['iconfont',detail.IsLike?'icon-like':'icon-unlike']"></li>
						<text>{{detail.LikeNum}}</text>
					</view>
					<view class="btn-item row_align_center" @tap.stop="handleComment(detail)">
						<li class="iconfont icon-comment"></li>
					</view>
				</view>
			</view>
			<view class="empty-line-10"></view>
			<view class="comment-wrap">
				<view class="scroll-wrap">
					<view class="top-num padding-tb">评论</view>
					<template v-if="detail.CommentList && detail.CommentList.length > 0">
						<view class="comment-item" v-for="(comItem, index) in detail.CommentList"
							:key="index">
							<view class="left-avatar">
								<image class="avatar" :src="comItem.AvatarUrl" mode="widthFix"></image>
							</view>
							<view class="right-box">
								<view class="right-mine">
									<view class="right-top">
										<p class="title">
											{{ comItem.SendName }}
											<!-- <text v-if="item.isAuthor" class="author">作者</text> -->
										</p>
									</view>
									<p class="content">
										<template v-if="comItem.ReceiverId > 0">
											<text class="relpy">回复</text>
											<text class="name">
												{{comItem.ReceiverName}}：
											</text>
										</template>
										<text @click="replyComment(comItem,index)">
											{{comItem.Content}}
										</text>
									</p>
								</view>
							</view>
						</view>
					</template>
					<template v-else>
						<view class="flex-center margin">
							<empty content="暂无评论" />
						</view>
					</template>
				</view>
				<!-- <view class="comment-input">
					<input type="text" v-model="commentVal" class="top-input" placeholder="说点什么吧..." />
					<view class="send-btn" @tap="addCommon">发送</view>
				</view> -->
			</view>
		</view>
		<!-- 评论 -->
		<eject-drawer mode="bottom" :visible="commentState" @close="cancleComment()" drawerWidth="100%"
			drawerHeight="40%">
			<my-comment ref="mycomment" :defaultPlaceholder="replyModel.receiverName?('回复'+replyModel.receiverName+'：'):'评论：'" @cancleComment="cancleComment" @commitComment="commitComment"></my-comment>
		</eject-drawer>
		<!-- 删除评论 -->
		<eject-drawer mode="bottom" :visible="deleteReplyStatus" @close="deleteReplyStatus=false" drawerWidth="100%"
			drawerHeight="20%" class="delete-box">
			<delete-comment @cancleDeletReply="cancleDeletReply" @deleteComment="deleteComment"></delete-comment>
		</eject-drawer>
	</view>
</template>

<script>
	var that;
	import myComment from "@/components/common/my-comment.vue"
	import ejectDrawer from '@/components/common/eject-drawer'
	import deleteComment from '@/components/community/delete-comment'
	import empty from '@/components/empty/empty'
	export default {
		components: {
			myComment,
			ejectDrawer,
			deleteComment,
			empty
		},
		data() {
			return {
				detailId: '',
				commentVal: '',
				current: 0,
				detail: {},
				imgList: [],
				commentState:false,
				commentCommunityId:0,
				replyModel:{
					receiverId:0,
					receiverName:"",
					type:0,
					communityImg:"",
					communityContent:""
				},
				deleteReplyStatus:false,
				deleteCommentId:0,
				deleteCommentIndex:-1
			};
		},
		onLoad(options) {
			if (options.str) {
				this.detail = JSON.parse(decodeURIComponent(options.str));
			}else if(options.id){
				this.detail = this.$store.state.communityList.filter(x=>x.Id == options.id)[0];
			}

			// 分享微信好友和朋友圈
			// #ifdef MP-WEIXIN
			uni.showShareMenu({
				withShareTicket: true,
				menus: ['shareAppMessage', 'shareTimeline']
			})
			// #endif
		},
		methods: {
			// 回复评论
			replyComment(obj,index){
				this.commentCommunityId = obj.CommunityId;
				if(obj.SendId == this.$store.state.sender.Id){
					this.deleteReplyStatus = true;
					this.deleteCommentId = obj.Id;
					this.deleteCommentIndex = index;
				}else{
					this.replyModel.receiverId = obj.SendId;
					this.replyModel.receiverName = obj.SendName;
					this.replyModel.type = 1;
					this.commentState = true;
					this.replyModel.communityImg = obj.CommunityImg;
					this.replyModel.communityContent = obj.Content;
				}
			},
			// 点击评论
			handleComment(item){
				this.commentState = true;
				this.commentCommunityId = item.Id;
				this.replyModel.receiverId = item.PublishId;
				this.replyModel.receiverName = "";
				this.replyModel.type = 0;
				this.replyModel.communityImg = item.ImgList.length > 0 ? item.ImgList[0] : "";
				this.replyModel.communityContent = item.Content;
			},
			// 点赞
			handleLike(e){
				// this.$emit('handleLike',item)
				let query = {
					userId : this.$store.state.sender.Id,
					communityId:e.Id
				}
				if(e.IsLike){
					this.$Request.post(this.$Config.APIURL + "/api/deleteCommunityLikeRecord", query).then(res => {
						if (res?.code == 200) {
							e.IsLike = false;
							if(res?.data != -1){
								e.LikeNum = res?.data;
							}else{
								e.LikeNum --;
							}
							let communityList = this.$store.state.communityList;
							communityList.map((x)=>{
								if(x.Id === e.Id){
									x.LikeNum = e.LikeNum;
									x.IsLike = e.IsLike;
								}
							})
							this.$store.commit("setPropName",{propName:'communityList',value:communityList});
							
						} else {
							this.$Common.tipMsg(res?.message);
						}
					});
				}else{
					this.$Request.post(this.$Config.APIURL + "/api/insertCommunityLikeRecord", query).then(res => {
						if (res?.code == 200) {
							e.IsLike = true;
							if(res?.data != -1){
								e.LikeNum = res?.data;
							}else{
								e.LikeNum ++;
							}
							let communityList = this.$store.state.communityList;
							communityList.map((x)=>{
								if(x.Id === e.Id){
									x.LikeNum = e.LikeNum;
									x.IsLike = e.IsLike;
								}
							})
							this.$store.commit("setPropName",{propName:'communityList',value:communityList});
						} else {
							this.$Common.tipMsg(res?.message);
						}
					});
				}
			},
			deleteComment(){
				if(!(this.deleteCommentId > 0)){
					this.$Common.tipMsg("缺乏参数")
					return;				
				}
				let model = {
					id:this.deleteCommentId
				};
				this.$Request.post(this.$Config.APIURL + "/api/deleteCommunityComment", model).then(res => {
					if (res?.code == 200) {
						this.detail.CommentList.splice(this.deleteCommentIndex,1);
						this.deleteReplyStatus = false;
					} else {
						this.$Common.tipMsg(res?.message);
					}
				});
			},
			cancleDeletReply(){
				this.deleteReplyStatus = false;
				this.deleteCommentId = 0;
				this.deleteCommentIndex = -1;
			},
			// 取消评论
			cancleComment(){
				this.commentState = false;
				this.commentCommunityId = 0;
				this.replyModel.receiverId = 0;
				this.replyModel.receiverName = "";
				this.replyModel.communityContent = "";
				this.replyModel.communityImg = "";
				this.replyModel.type = 0;
			},
			// 全屏预览
			ViewImage(index, arr) {
				uni.previewImage({
					current: index,
					urls: arr
				});
			},
			// 提交评论
			commitComment(e){
				let model = {
					communityId:this.commentCommunityId,
					sendId:this.$store.state.sender.Id,
					sendName:this.$store.state.sender.Name,
					receiverId:this.replyModel.receiverId,
					receiverName:this.replyModel.receiverName,
					type:this.replyModel.type,
					avatarUrl:this.$store.state.sender.Avatar,
					content:e,
					communityImg:this.replyModel.communityImg,
					communityContent:this.replyModel.communityContent
				}
				this.$Request.post(this.$Config.APIURL + "/api/insertCommunityComment", model).then(res => {
					if (res?.code == 200) {
						this.$refs.mycomment.comment = "";
						let id = this.commentCommunityId
						this.cancleComment();
						this.getCommunityComment(id);
					} else {
						this.$Common.tipMsg(res?.message);
					}
				});
			},
			// 获去评论列表
			getCommunityComment(id){
				let model = {
					communityId:id
				}
				this.$Request.post(this.$Config.APIURL + "/api/getCommunityCommentList", model).then(res => {
					if (res?.code == 200) {
						this.detail.CommentList = res?.data;
						let communityList = this.$store.state.communityList;
						communityList.map((x)=>{
							if(x.Id === id){
								x.CommentList = this.detail.CommentList;
							}
						})
						this.$store.commit("setPropName",{propName:'communityList',value:communityList});
					} else {
						this.$Common.tipMsg(res?.message);
					}
				});
			},
			swiperChange(e) {
				this.current = e.detail.current;
			}
		}
	};
</script>

<style lang="scss" scoped>
	.radius {
		border-radius: 16rpx;
	}
	.icon-like{
		color: #FFCB57;
	}
	.name{
		color: #526B94;
	}

	.empty-line-10 {
		background-color: #E1E1E1;
	}

	.card-item {
		// background-color: #ffffff;
		// box-shadow: 0 20rpx 40rpx 0 rgba(0, 0, 0, 0.1);

		.item-head {
			display: flex;
			align-items: center;
			width: 100%;
			justify-content: space-between;
			margin: 20rpx 0;

			.left-info {
				display: flex;
				align-items: center;

				.img-wrap {
					width: 80rpx;
					height: 80rpx;
					overflow: hidden;
					border-radius: 50%;

					.avatar {
						width: 100%;
						height: 80rpx;
					}
				}

				.info {
					padding-left: 10rpx;
					text-align: left;

					.head-name {
						color: #666666;
						font-size: 28rpx;
					}

					.head-create {
						color: #999999;
						font-size: 20rpx;
					}
				}
			}

			.follow-btn {
				width: 140rpx;
				height: 60rpx;
				line-height: 60rpx;
				border: 1rpx solid #CCCCCC;
				border-radius: 40rpx;
				text-align: center;
			}

			.followed {
				color: #fff;
				background: $primary-color;
				border: 1rpx solid $primary-color;
				width: 140rpx;
				height: 60rpx;
				border-radius: 30rpx;
				text-align: center;
				line-height: 60rpx;
			}
		}

		.content {
			padding: 20rpx 30rpx 0rpx;

			.content-other {
				width: 100%;
				height: 40px;
				font-size: 28rpx;
				font-weight: 400;
				color: #333333;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.like {
					display: flex;
					align-items: center;

					.icon-liked {
						color: #FFAF36;
					}
				}
			}

			.text-content {
				line-height: 40rpx;
				justify-content: space-between;
				font-size: 30rpx;
				text-align: justify;
			}

			.padding-bottom-lg {
				padding-bottom: 40upx;
			}


			.img-list {
				display: flex;
				flex-flow: row wrap;

				.img-box {
					flex: 0 0 30%;
					margin: 0 1% 1%;
					display: flex;
					align-items: center;
					justify-content: center;
					overflow: hidden;
					border-radius: 4px;
					background-color: #ececec;
					margin-top: 26rpx;

					.img {
						width: 100%;
					}
				}
			}

			.img-wrap {
				.img-box {
					width: 100%;
					max-height: 400rpx;
					overflow: hidden;
					display: flex;
					align-items: center;
					justify-content: center;
					border-radius: 30rpx;
					margin-top: 26rpx;

					.img {
						width: 100%;
					}
				}
			}

			.bottom-btn {
				padding: 20upx;
				justify-content: space-around;
				font-size: 28rpx;
				color: #999999;

				.btn-item {
					width: 33%;
					display: flex;
					align-items: center;
					color: #435257;

					&:nth-child(2) {
						justify-content: center;
					}

					&:nth-child(3) {
						justify-content: flex-end;
					}

					li {
						margin-right: 20rpx;
						font-size: 22px !important;
						font-weight: 600;
					}
				}
			}
		}
	}

	view,
	scroll-view,
	swiper,
	button,
	input,
	image {
		box-sizing: border-box;
	}

	.detail-wrap {
		background-color: #ffffff;
		// padding: 120rpx 0 100rpx;
		// margin-bottom: 100rpx;

		.item-head {


			.left-info {
				display: flex;
				align-items: center;

				.img-wrap {
					width: 80rpx;
					height: 80rpx;
					overflow: hidden;
					border-radius: 50%;
					border: 1rpx solid #ECECEC;

					.avatar {
						width: 100%;
					}
				}

				.head-name {
					color: #666666;
					font-size: 32rpx;
				}
			}
		}

		.swiper-wrap {
			position: relative;

			.swiper-box {
				height: 500rpx;
			}

			.custom-indicator {
				position: absolute;
				right: 10px;
				top: 10px;
				background: rgba(0, 0, 0, 0.5);
				padding: 2px 5px;
				border-radius: 10px;
				color: #fff;
			}

			.img {
				width: 100%;
				display: inline-block;
				position: relative;
				z-index: 0;
			}
		}

		.content-box {
			padding: 0 40rpx;
			font-size: 32rpx;
			line-height: 50rpx;
		}

		.bottom-time {
			padding: 20rpx 40rpx;
			font-size: 24rpx;
			color: #999999;
		}

		.comment-wrap {
			background-color: #fff;
			padding: 0 20rpx 20rpx;
			margin-bottom: 100rpx;

			.scroll-wrap {
				height: 100%;

				.top-num {
					text-align: left;
					height: 90rpx;
					font-size: 18px;
					color: #333333;
					line-height: 90rpx;
				}
			}

			.comment-item {
				display: flex;
				justify-content: space-between;
				margin-bottom: 40rpx;

				.left-avatar {
					width: 70rpx;
					height: 70rpx;
					border-radius: 50%;
					overflow: hidden;
					border: 1rpx solid #ECECEC;

					.avatar {
						width: 100%;
					}
				}

				.right-box {
					margin-left: 20rpx;
					flex: 1;

					.right-mine {
						border-radius: 10rpx;
						flex: 1;
					}

					.right-top {
						display: flex;
						justify-content: space-between;

						.reply {
							display: flex;
							font-size: 28rpx;
						}

						.active {
							color: #36b39b !important;
						}

						.title {
							flex: 1;
							color: #526B94;
							text-align: left;

							.author {
								background-color: $primary-color;
								color: #ffffff;
								padding: 0 8rpx;
								margin-left: 10rpx;
								border-radius: 6rpx;
							}
						}

						.time {
							color: #999;
						}

						.like {
							display: flex;
							align-items: center;

							.icon-liked {
								color: #FFAF36;
							}
						}
					}

					.content {
						padding: 10rpx 0;
						text-align: left;
						.relpy{
							padding: 0 8rpx;
						}
					}
				}
			}
		}

		.comment-input {
			display: flex;
			align-items: center;
			justify-content: space-between;
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			background-color: #fff;
			padding: 20rpx 40rpx;
			border-top: 1rpx solid #ECECEC;

			.top-input {
				width: 100%;
				height: 60rpx;
				display: flex;
				align-items: center;
				padding: 0 40rpx;
				background-color: #f5f5f5;
				color: #000;
				border-radius: 80rpx;
				flex: 1;
			}

			.send-btn {
				display: flex;
				align-items: center;
				justify-content: center;
				height: 60rpx;
				width: 140rpx;
				margin-left: 20rpx;
				border-radius: 40rpx;
				border: 1rpx solid $primary-color;
				color: $primary-color;
				background-color: #ffffff;
			}
		}
	}
</style>
